package com.laozj.test.controller;

import com.laozj.common.utils.R;
import com.laozj.test.dao.UserDao;
import com.laozj.test.entity.StudentEntity;
import com.laozj.test.entity.UserEntity;
import com.laozj.test.service.StudentService;
import com.laozj.test.service.UserService;
import com.laozj.test.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    //每个线程进来。age-2,三种方案，保证不能让age<0
    /**
     * 乐观锁 测试
     * @return
     */
    @PostMapping("/leGuanLock")
    public R leGuanLock(@RequestBody UserVO userVO){
        boolean ans = userService.leGuanLock(userVO);
        if(!ans){
            return R.error("操作失败");
        }
        return R.ok("操作成功");
    }


    /**
     * 悲观锁 测试
     * @return
     */
    @PostMapping("/peiGuanLock")
    public R peiGuanLock(@RequestBody UserVO userVO){
        boolean ans =  userService.peiGuanLock(userVO);
        if(!ans){
            return R.error("操作失败");
        }
        return R.ok("操作成功");
    }


    /**
     * redis+RabbitMQ 测试
     * @return
     */
    @PostMapping("/RBTest")
    public R RBLock(@RequestBody UserVO userVO){
        boolean ans =  userService.RBLock(userVO);
        if(!ans){
            return R.error("操作失败");
        }
        return R.ok("操作成功");
    }


    @PostMapping("/list")
    public R list(@RequestBody UserVO queryVO){
        List<UserEntity> entityList =  userService.getUserList(queryVO);
        return R.ok("查找成功").setDate(null);
    }

    @PostMapping("/update")
    public R update(@RequestBody List<UserVO> userVOList){
        List<UserVO> update = userService.update(userVOList);
        return R.ok("更新成功");
    }


    @PostMapping("/save")
    public R save(@RequestBody List<UserVO> userVOList){
        userService.save(userVOList);
        return R.ok("更新成功");
    }

}
